package club.caihappy.store.controller;

import club.caihappy.store.entity.Address;
import club.caihappy.store.service.IAddressService;
import club.caihappy.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by DELL on 2022/3/16
 **/
@RestController
@RequestMapping("addresses")
public class AddressController extends BaseController {

    @Autowired
    private IAddressService addressService;

    @RequestMapping("add_new_address")
    public JsonResult<Void> addNewAddress(HttpSession session, Address address) {
        addressService.addNewAddress(getUidFromSession(session), getUsernameFromSession(session), address);
        return new JsonResult<>(OK, "收货地址添加成功");
    }

    @RequestMapping({"", "/"})
    public JsonResult<List<Address>> findAllAddress(HttpSession session) {
        List<Address> addressList = addressService.getByUid(getUidFromSession(session));
        return new JsonResult<>(OK, addressList);
    }

    @RequestMapping("set_default/{aid}")
    public JsonResult<Void> setDefault(HttpSession session, @PathVariable("aid") Integer aid) {
        addressService.setDefault(aid, getUidFromSession(session), getUsernameFromSession(session));
        return new JsonResult<>(OK, "修改成功");
    }

    @RequestMapping("delete/{aid}")
    public JsonResult<Void> delete(@PathVariable("aid") Integer aid, HttpSession session) {
        addressService.deleteAddress(aid, getUidFromSession(session), getUsernameFromSession(session));
        return new JsonResult<>(OK, "删除成功");
    }

}
